package com.dcivision.dms.core;

import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;

import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.bean.DmsDocumentDetail;
import com.dcivision.dms.extractor.AbstractTextExtractor;
import com.dcivision.dms.extractor.ExcelTextExtractor;
import com.dcivision.dms.extractor.HtmlTextExtractor;
import com.dcivision.dms.extractor.PdfTextExtractor;
import com.dcivision.dms.extractor.PowerPointTextExtractor;
import com.dcivision.dms.extractor.RtfTextExtractor;
import com.dcivision.dms.extractor.StandardTextExtractor;
import com.dcivision.dms.extractor.WordTextExtractor;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.FullTextIndexFactory;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.IndexManager;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;

/* for index the document profile field into the DMS_DOCUMENT_INDEX */
//import com.dcivision.dms.*;
//import com.dcivision.dms.bean.*;
//import com.dcivision.dms.core.*;

/**
 * <p>Class Name:       DmsIndexConvertor.java    </p>
 * <p>Description:      This class mainly convert the dmsDocument related information to lucene doc object.</p>
 * @author              Rollo Chan
 * @company             DCIVision Limited
 * @creation date       08/08/2003
 * @version             $Revision: 1.69.2.1 $
 * @deprecated This class has deprecated.
 */
public class DmsIndexConverter {

  public static final String REVISION = "$Revision: 1.69.2.1 $";

  public static HashMap hashExtractor = new HashMap();
  private static final Log log = LogFactory.getLog(DmsIndexConverter.class);
  // Associated with the text extractor with different type of files.
  static {
    hashExtractor.put("PPT", PowerPointTextExtractor.class);
    hashExtractor.put("XLS", ExcelTextExtractor.class);
    hashExtractor.put("HTM", HtmlTextExtractor.class);
    hashExtractor.put("HTML", HtmlTextExtractor.class);
    hashExtractor.put("PDF", PdfTextExtractor.class);
    hashExtractor.put("RTF", RtfTextExtractor.class);
    hashExtractor.put("DOC", WordTextExtractor.class);
//    hashExtractor.put("DOC", ASPWordTextExtractor.class);  // Temporary purpose for trail by ASP extraction
    hashExtractor.put("TXT", StandardTextExtractor.class);
    hashExtractor.put("SQL", StandardTextExtractor.class);
    hashExtractor.put("LOG", StandardTextExtractor.class);
    hashExtractor.put("PHP", StandardTextExtractor.class);
    hashExtractor.put("JSP", StandardTextExtractor.class);
    hashExtractor.put("ASP", StandardTextExtractor.class);
    hashExtractor.put("CSV", StandardTextExtractor.class);
    hashExtractor.put("XML", StandardTextExtractor.class);
    hashExtractor.put("VCF", StandardTextExtractor.class);
    hashExtractor.put("VCS", StandardTextExtractor.class);
    hashExtractor.put("CONFIG", StandardTextExtractor.class);
    hashExtractor.put("CONF", StandardTextExtractor.class);
    hashExtractor.put("PROPERTIES", StandardTextExtractor.class);
    hashExtractor.put("JAVA", StandardTextExtractor.class);
    hashExtractor.put("H", StandardTextExtractor.class);
    hashExtractor.put("CC", StandardTextExtractor.class);
    hashExtractor.put("CPP", StandardTextExtractor.class);
    hashExtractor.put("INI", StandardTextExtractor.class);
  }

  protected DmsIndexConverter() {
    super();
  }


  /**
   * Converts dmsDocument related information into lucene doc object include dmsDocument object fields,
   * dmsDocumentDetails and Full Text.
   * <pre>
   *   Lucene doc. field added:
   *   Extra index field for sorting
   *   1. IndexManager.INDEX_NAME_DOC_NAME_SORT - Document name
   *   2. IndexManager.INDEX_NAME_ITEM_SIZE     - Document item size
   *   3. IndexManager.INDEX_NAME_ITEM_STATUS   - Document item status
   *   4. IndexManager.INDEX_NAME_OWNER_ID      - Document owner ID
   *   5. IndexManager.INDEX_NAME_RECORD_STATUS - Doucment record status (ie. in double digit format e.g
   *                                              (AA - active, II - inactive)
   *   Document field for index
   *   1.  PARENT_ID                             - Parent id of the document
   *   2.  ROOT_ID                               - Root id of the document
   *   3.  PRIORITY                              - Priority of the document
   *   4.  DOCUMENT_NAME                         - Document name
   *   5.  DOCUMENT_TYPE                         - Document Type (Z,D,C,H,L)
   *   6.  EFFECTIVE_START_DATE                  - Effective start date
   *   7.  EFFECTIVE_END_DATE                    - Effective end date
   *   8.  USER_DEF_1                            - default profile field value 1
   *   9.  USER_DEF_2                            - default profile field value 2
   *   10. USER_DEF_3                            - default profile field value 3
   *   11. USER_DEF_4                            - default profile field value 4
   *   12. USER_DEF_5                            - default profile field value 5
   *   13. USER_DEF_6                            - default profile field value 6
   *   14. USER_DEF_7                            - default profile field value 7
   *   15. USER_DEF_8                            - default profile field value 8
   *   16. USER_DEF_9                            - default profile field value 9
   *   17. USER_DEF_10                           - default profile field value 10
   *   18. DESCRIPTION                           - Document description
   *   19. REFERENCE_NO                          - Document reference no.
   *   20. VERSION_PARENT_ID                     - Version parent ID
   *   21. VERSION_ID                            - Version ID
   *   22. VERSION_NUMBER                        - Version Number
   *   23. VERSION_LABEL                         - Version Label
   *   24. UDF_ID                                - User defined profile ID
   *   25. UDF_(*)                               - User defined profile detail value, (*) is the detail ID
   *   26. ALL_RELATED_INDEXES                   - All document related information
   *   27. FULL_TEXT                             - Full text extract from document content.
   *   
   *   
   * </pre>
   * 
   * @param dmsDocument the dmsDocument object
   * @param data        the InputStream of the document content
   * @param fields      Lucene field name
   * @return Lucene document
   * @throws ApplicationException
   * @see com.dcivision.framework.IndexManager
   * @see com.dcivision.dms.bean.DmsDocument
   */
  public static Document getIndexDocument(DmsDocument dmsDocument, InputStream data, String[] fields) throws ApplicationException {
    Document doc = FullTextIndexFactory.getInstance().getBasicDocument(dmsDocument, GlobalConstant.OBJECT_TYPE_DOCUMENT);
    java.util.Hashtable hashTmp = new java.util.Hashtable();
    
    //define the ALL_RELATED_INDEXES
    //String allRelatedIndexs =TextUtility.noNull(dmsDocument.getDocumentName())+TextUtility.noNull(TextUtility.formatIntegerObj(dmsDocument.getParentID()))+TextUtility.noNull(TextUtility.formatIntegerObj(dmsDocument.getRootID()))+TextUtility.noNull(dmsDocument.getPriority())+TextUtility.noNull(dmsDocument.getDocumentType())+TextUtility.noNull(dmsDocument.getUserDef1())+TextUtility.noNull(dmsDocument.getUserDef2())+TextUtility.noNull(dmsDocument.getUserDef3())+TextUtility.noNull(dmsDocument.getUserDef4())+TextUtility.noNull(dmsDocument.getUserDef5())+TextUtility.noNull(dmsDocument.getUserDef6())+TextUtility.noNull(dmsDocument.getUserDef7())+TextUtility.noNull(dmsDocument.getUserDef8())+TextUtility.noNull(dmsDocument.getUserDef9())+TextUtility.noNull(dmsDocument.getUserDef10())+TextUtility.noNull(dmsDocument.getDescription())+TextUtility.noNull(dmsDocument.getReferenceNo())+TextUtility.noNull(TextUtility.formatIntegerObj(dmsDocument.getVersionParentID()))+TextUtility.noNull(TextUtility.formatIntegerObj(dmsDocument.getVersionID()))+TextUtility.noNull(dmsDocument.getVersionNumber())+TextUtility.noNull(dmsDocument.getVersionLabel())+TextUtility.noNull(TextUtility.formatTimestampToIndex(Utility.stripToMinutes(dmsDocument.getUpdateDate())))+TextUtility.noNull(dmsDocument.getUpdaterName().toLowerCase())+TextUtility.noNull(dmsDocument.getUpdaterID().toString())+TextUtility.noNull(TextUtility.formatTimestampToIndex(Utility.stripToMinutes(dmsDocument.getCreateDate())))+TextUtility.noNull(dmsDocument.getCreatorName())+TextUtility.noNull(dmsDocument.getCreatorID().toString())+TextUtility.noNull(dmsDocument.getRecordStatus())+TextUtility.noNull((dmsDocument.getRecordStatus()).toLowerCase());
    String allRelatedIndexs = "";    

    if (fields != null) {
      for (int i = 0; i < fields.length; i++) {
        hashTmp.put(fields[i], "Y");
      }
    }

    //extra index added for sorting
    if(fields == null || (hashTmp.get(IndexManager.INDEX_NAME_DOC_NAME_SORT)!=null)){
      doc.add(new Field(IndexManager.INDEX_NAME_DOC_NAME_SORT, TextUtility.noNull(dmsDocument.getDocumentName()),Field.Store.YES, Field.Index.UN_TOKENIZED));
      //allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(dmsDocument.getDocumentName()) +" ";
    }

    if(fields == null || (hashTmp.get(IndexManager.INDEX_NAME_ITEM_SIZE)!=null)){
      doc.add(new Field(IndexManager.INDEX_NAME_ITEM_SIZE, TextUtility.noNull(IndexManager.numberPad(dmsDocument.getItemSize())),Field.Store.YES, Field.Index.UN_TOKENIZED));
      //allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(IndexManager.numberPad(dmsDocument.getItemSize())) +" ";
    }
    if(fields == null || (hashTmp.get(IndexManager.INDEX_NAME_ITEM_STATUS)!=null)){
      doc.add(new Field(IndexManager.INDEX_NAME_ITEM_STATUS, TextUtility.noNull(dmsDocument.getItemStatus()),Field.Store.YES, Field.Index.UN_TOKENIZED));
      //allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(dmsDocument.getItemStatus()) +" ";
    }
    if(fields == null || (hashTmp.get(IndexManager.INDEX_NAME_OWNER_ID)!=null)){
      doc.add(new Field(IndexManager.INDEX_NAME_OWNER_ID, TextUtility.noNull(TextUtility.formatIntegerObj(dmsDocument.getOwnerID())),Field.Store.YES, Field.Index.UN_TOKENIZED));
      //allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(TextUtility.formatIntegerObj(dmsDocument.getOwnerID())) +" ";
    }
    
    //update status for workflow 
    if (fields == null || hashTmp.get(IndexManager.INDEX_NAME_RECORD_STATUS) != null) {
      doc.add(new Field(IndexManager.INDEX_NAME_RECORD_STATUS, TextUtility.noNull(dmsDocument.getRecordStatus())+TextUtility.noNull(dmsDocument.getRecordStatus()),Field.Store.YES, Field.Index.UN_TOKENIZED));
      //allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(dmsDocument.getRecordStatus())+TextUtility.noNull(dmsDocument.getRecordStatus()) +" ";
    }


    // Document specified fields to be indexed.    
    if (fields == null || hashTmp.get("PARENT_ID") != null) {
      doc.add(new Field("PARENT_ID", TextUtility.noNull(TextUtility.formatIntegerObj(dmsDocument.getParentID())),Field.Store.YES, Field.Index.UN_TOKENIZED));
    }
    if (fields == null || hashTmp.get("ROOT_ID") != null) {
      doc.add(new Field("ROOT_ID", TextUtility.noNull(TextUtility.formatIntegerObj(dmsDocument.getRootID())),Field.Store.YES, Field.Index.UN_TOKENIZED));
    }
    if (fields == null || hashTmp.get("PRIORITY") != null) {
      doc.add(new Field("PRIORITY", TextUtility.noNull(dmsDocument.getPriority()),Field.Store.YES, Field.Index.UN_TOKENIZED));
      allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(dmsDocument.getPriority()) +" ";
    }
    if (fields == null || hashTmp.get("DOCUMENT_NAME") != null) {
      doc.add(new Field("DOCUMENT_NAME", TextUtility.noNull(dmsDocument.getDocumentName()),Field.Store.YES, Field.Index.UN_TOKENIZED));
      doc.add(new Field("DOCUMENT_NAME", TextUtility.noNull(dmsDocument.getDocumentName()),Field.Store.YES, Field.Index.TOKENIZED));
      allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(dmsDocument.getDocumentName()) +" ";
    }
    if (fields == null || hashTmp.get("DOCUMENT_TYPE") != null) {
      doc.add(new Field("DOCUMENT_TYPE", TextUtility.noNull(dmsDocument.getDocumentType()),Field.Store.YES, Field.Index.UN_TOKENIZED));
      allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(dmsDocument.getDocumentType()) +" ";
    }
    if (fields == null || hashTmp.get("EFFECTIVE_START_DATE") != null) {
      if (dmsDocument.getEffectiveStartDate() != null) {
       
        //doc.add(new Field("EFFECTIVE_START_DATE", TextUtility.noNull(DateField.dateToString(Utility.stripToMinutes( dmsDocument.getEffectiveStartDate()))),Field.Store.YES, Field.Index.UN_TOKENIZED));
        doc.add(new Field("EFFECTIVE_START_DATE", TextUtility.noNull(TextUtility.formatTimestampToIndex(Utility.stripToMinutes( dmsDocument.getEffectiveStartDate()))),Field.Store.YES, Field.Index.UN_TOKENIZED));
        //allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(TextUtility.formatTimestampToIndex(Utility.stripToMinutes( dmsDocument.getEffectiveStartDate()))) +" ";
      }
    }
    if (fields == null || hashTmp.get("EFFECTIVE_END_DATE") != null) {
      if (dmsDocument.getEffectiveEndDate() == null) {
        //doc.add(new Field("EFFECTIVE_END_DATE", TextUtility.noNull(DateField.MAX_DATE_STRING()),Field.Store.YES, Field.Index.UN_TOKENIZED));
        doc.add(new Field("EFFECTIVE_END_DATE", TextUtility.noNull(IndexManager.MAX_DATE_VALUE),Field.Store.YES, Field.Index.UN_TOKENIZED));
        //allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(IndexManager.MAX_DATE_VALUE) +" ";
      } else {
        //doc.add(new Field("EFFECTIVE_END_DATE", TextUtility.noNull(DateField.dateToString(Utility.stripToMinutes(dmsDocument.getEffectiveEndDate()))),Field.Store.YES, Field.Index.UN_TOKENIZED));
        doc.add(new Field("EFFECTIVE_END_DATE", TextUtility.noNull(TextUtility.formatTimestampToIndex(Utility.stripToMinutes(dmsDocument.getEffectiveEndDate()))),Field.Store.YES, Field.Index.UN_TOKENIZED));
        //allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(TextUtility.formatTimestampToIndex(Utility.stripToMinutes(dmsDocument.getEffectiveEndDate()))) +" ";
      }
    }
    if (fields == null || hashTmp.get("USER_DEF_1") != null) {
      doc.add(new Field("USER_DEF_1", TextUtility.noNull(dmsDocument.getUserDef1()),Field.Store.YES, Field.Index.UN_TOKENIZED));
      doc.add(new Field("USER_DEF_1", TextUtility.noNull(dmsDocument.getUserDef1()),Field.Store.YES, Field.Index.TOKENIZED));
      allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(dmsDocument.getUserDef1()) +" ";
    }
    if (fields == null || hashTmp.get("USER_DEF_2") != null) {
      doc.add(new Field("USER_DEF_2", TextUtility.noNull(dmsDocument.getUserDef2()),Field.Store.YES, Field.Index.UN_TOKENIZED));
      doc.add(new Field("USER_DEF_2", TextUtility.noNull(dmsDocument.getUserDef2()),Field.Store.YES, Field.Index.TOKENIZED));
      allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(dmsDocument.getUserDef2()) +" ";
    }
    if (fields == null || hashTmp.get("USER_DEF_3") != null) {
      doc.add(new Field("USER_DEF_3", TextUtility.noNull(dmsDocument.getUserDef3()),Field.Store.YES, Field.Index.UN_TOKENIZED));
      doc.add(new Field("USER_DEF_3", TextUtility.noNull(dmsDocument.getUserDef3()),Field.Store.YES, Field.Index.TOKENIZED));
      allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(dmsDocument.getUserDef3()) +" ";
    }
    if (fields == null || hashTmp.get("USER_DEF_4") != null) {
      doc.add(new Field("USER_DEF_4", TextUtility.noNull(dmsDocument.getUserDef4()),Field.Store.YES, Field.Index.UN_TOKENIZED));
      doc.add(new Field("USER_DEF_4", TextUtility.noNull(dmsDocument.getUserDef4()),Field.Store.YES, Field.Index.TOKENIZED));
      allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(dmsDocument.getUserDef4()) +" ";
    }
    if (fields == null || hashTmp.get("USER_DEF_5") != null) {
      doc.add(new Field("USER_DEF_5", TextUtility.noNull(dmsDocument.getUserDef5()),Field.Store.YES, Field.Index.UN_TOKENIZED));
      doc.add(new Field("USER_DEF_5", TextUtility.noNull(dmsDocument.getUserDef5()),Field.Store.YES, Field.Index.TOKENIZED));
      allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(dmsDocument.getUserDef5()) +" ";
    }
    if (fields == null || hashTmp.get("USER_DEF_6") != null) {
      doc.add(new Field("USER_DEF_6", TextUtility.noNull(dmsDocument.getUserDef6()),Field.Store.YES, Field.Index.UN_TOKENIZED));
      doc.add(new Field("USER_DEF_6", TextUtility.noNull(dmsDocument.getUserDef6()),Field.Store.YES, Field.Index.TOKENIZED));
      allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(dmsDocument.getUserDef6()) +" ";
    }
    if (fields == null || hashTmp.get("USER_DEF_7") != null) {
      doc.add(new Field("USER_DEF_7", TextUtility.noNull(dmsDocument.getUserDef7()),Field.Store.YES, Field.Index.UN_TOKENIZED));
      doc.add(new Field("USER_DEF_7", TextUtility.noNull(dmsDocument.getUserDef7()),Field.Store.YES, Field.Index.TOKENIZED));
      allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(dmsDocument.getUserDef7()) +" ";
    }
    if (fields == null || hashTmp.get("USER_DEF_8") != null) {
      doc.add(new Field("USER_DEF_8", TextUtility.noNull(dmsDocument.getUserDef8()),Field.Store.YES, Field.Index.UN_TOKENIZED));
      doc.add(new Field("USER_DEF_8", TextUtility.noNull(dmsDocument.getUserDef8()),Field.Store.YES, Field.Index.TOKENIZED));
      allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(dmsDocument.getUserDef8()) +" ";
    }
    if (fields == null || hashTmp.get("USER_DEF_9") != null) {
      doc.add(new Field("USER_DEF_9", TextUtility.noNull(dmsDocument.getUserDef9()),Field.Store.YES, Field.Index.UN_TOKENIZED));
      doc.add(new Field("USER_DEF_9", TextUtility.noNull(dmsDocument.getUserDef9()),Field.Store.YES, Field.Index.TOKENIZED));
      allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(dmsDocument.getUserDef9()) +" ";
    }
    if (fields == null || hashTmp.get("USER_DEF_10") != null) {
      doc.add(new Field("USER_DEF_10", TextUtility.noNull(dmsDocument.getUserDef10()),Field.Store.YES, Field.Index.UN_TOKENIZED));
      doc.add(new Field("USER_DEF_10", TextUtility.noNull(dmsDocument.getUserDef10()),Field.Store.YES, Field.Index.TOKENIZED));
      allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(dmsDocument.getUserDef10()) +" ";
    }
    if (fields == null || hashTmp.get("DESCRIPTION") != null) {
      doc.add(new Field("DESCRIPTION", TextUtility.noNull(dmsDocument.getDescription()),Field.Store.YES, Field.Index.UN_TOKENIZED));
      doc.add(new Field("DESCRIPTION", TextUtility.noNull(dmsDocument.getDescription()).toLowerCase(),Field.Store.YES, Field.Index.TOKENIZED));
      allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(dmsDocument.getDescription()).toLowerCase() +" ";
    }
    if (fields == null || hashTmp.get("REFERENCE_NO") != null) {
      doc.add(new Field("REFERENCE_NO", TextUtility.noNull(dmsDocument.getReferenceNo()),Field.Store.YES, Field.Index.UN_TOKENIZED));
      doc.add(new Field("REFERENCE_NO", TextUtility.noNull(dmsDocument.getReferenceNo()),Field.Store.YES, Field.Index.TOKENIZED));
      allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(dmsDocument.getReferenceNo()) +" ";
    }
    if (fields == null || hashTmp.get("VERSION_PARENT_ID") != null) {
      doc.add(new Field("VERSION_PARENT_ID", TextUtility.noNull(TextUtility.formatIntegerObj(dmsDocument.getVersionParentID())),Field.Store.YES, Field.Index.UN_TOKENIZED));
      //allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(TextUtility.formatIntegerObj(dmsDocument.getVersionParentID())) +" ";
    }
    if (fields == null || hashTmp.get("VERSION_ID") != null) {
      doc.add(new Field("VERSION_ID", TextUtility.noNull(TextUtility.formatIntegerObj(dmsDocument.getVersionID())),Field.Store.YES, Field.Index.UN_TOKENIZED));
      //allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(TextUtility.formatIntegerObj(dmsDocument.getVersionID())) +" ";
    }
    if (fields == null || hashTmp.get("VERSION_NUMBER") != null) {
      doc.add(new Field("VERSION_NUMBER", TextUtility.noNull(dmsDocument.getVersionNumber()),Field.Store.YES, Field.Index.UN_TOKENIZED));
      //allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(dmsDocument.getVersionNumber()) +" ";
    }
    if (fields == null || hashTmp.get("VERSION_LABEL") != null) {
      doc.add(new Field("VERSION_LABEL", TextUtility.noNull(dmsDocument.getVersionLabel()),Field.Store.YES, Field.Index.UN_TOKENIZED));
      //allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(dmsDocument.getVersionLabel()) +" ";
    }
    if (fields == null || hashTmp.get("UDF_ID") != null) {
      if (dmsDocument.getUserDefinedFieldID()!=null) {
        doc.add(new Field("UDF_ID", TextUtility.formatIntegerObj(dmsDocument.getUserDefinedFieldID()),Field.Store.YES, Field.Index.UN_TOKENIZED));
        allRelatedIndexs = allRelatedIndexs + TextUtility.formatIntegerObj(dmsDocument.getUserDefinedFieldID()) +" ";
        if (dmsDocument.getDocumentDetails()!=null) {
          for (int i = 0; i < dmsDocument.getDocumentDetails().size(); i++) {
            DmsDocumentDetail dmsDocDetail = (DmsDocumentDetail)dmsDocument.getDocumentDetails().get(i);
            if (!Utility.isEmpty(dmsDocDetail.getFieldValue()) || dmsDocDetail.getDateValue() != null ||
             dmsDocDetail.getToDateValue() != null || dmsDocDetail.getNumericValue() != null ||
             dmsDocDetail.getToNumericValue() != null) {

              if (!Utility.isEmpty(dmsDocDetail.getFieldValue())) {


                doc.add(new Field("UDF_" + dmsDocDetail.getUserDefinedFieldDetailID(), TextUtility.noNull(dmsDocDetail.getFieldValue()).toLowerCase(),Field.Store.YES, Field.Index.UN_TOKENIZED));
                doc.add(new Field("UDF_" + dmsDocDetail.getUserDefinedFieldDetailID(), TextUtility.noNull(dmsDocDetail.getFieldValue()).toLowerCase(),Field.Store.YES, Field.Index.TOKENIZED));
                allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(dmsDocDetail.getFieldValue()).toLowerCase() +" ";
              }else if(!Utility.isEmpty(dmsDocDetail.getNumericValue())){

                doc.add(new Field("UDF_" + dmsDocDetail.getUserDefinedFieldDetailID(), TextUtility.noNull(IndexManager.numberPad(dmsDocDetail.getNumericValue())),Field.Store.YES, Field.Index.UN_TOKENIZED));
                doc.add(new Field("UDF_" + dmsDocDetail.getUserDefinedFieldDetailID(), TextUtility.noNull(IndexManager.numberPad(dmsDocDetail.getNumericValue())),Field.Store.YES, Field.Index.TOKENIZED));
                allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(IndexManager.numberPad(dmsDocDetail.getNumericValue())) +" ";

              }else if(!Utility.isEmpty(dmsDocDetail.getDateValue())){
                
                doc.add(new Field("UDF_" + dmsDocDetail.getUserDefinedFieldDetailID(), TextUtility.noNull(TextUtility.formatTimestampToIndex(dmsDocDetail.getDateValue())),Field.Store.YES, Field.Index.UN_TOKENIZED));
                doc.add(new Field("UDF_" + dmsDocDetail.getUserDefinedFieldDetailID(), TextUtility.noNull(TextUtility.formatTimestampToIndex(dmsDocDetail.getDateValue())),Field.Store.YES, Field.Index.TOKENIZED));
                //doc.add(new Field("UDF_" + dmsDocDetail.getUserDefinedFieldDetailID(), TextUtility.noNull(DateField.dateToString(dmsDocDetail.getDateValue())),Field.Store.YES, Field.Index.UN_TOKENIZED));
                //doc.add(new Field("UDF_" + dmsDocDetail.getUserDefinedFieldDetailID(), TextUtility.noNull(DateField.dateToString(dmsDocDetail.getDateValue())),Field.Store.YES, Field.Index.UN_TOKENIZED));
                allRelatedIndexs = allRelatedIndexs + TextUtility.noNull(TextUtility.formatTimestampToIndex(dmsDocDetail.getDateValue())) +" ";
              }
            }

          }
        }
      }
    }
    
    if (fields == null || hashTmp.get("ALL_RELATED_INDEXES") != null) {
      //doc.add(new Field("ALL_RELATED_INDEXES", TextUtility.noNull(allRelatedIndexs),Field.Store.YES, Field.Index.UN_TOKENIZED));
      doc.add(new Field("ALL_RELATED_INDEXES", TextUtility.noNull(allRelatedIndexs),Field.Store.YES, Field.Index.TOKENIZED));
    }

    // Full text index.
    if (data!=null) {
      AbstractTextExtractor extractor = null;
      String extension = null;
      if((!Utility.isEmpty(dmsDocument.getVersionFileName())) && 
       (!dmsDocument.getVersionFileName().equals(dmsDocument.getDocumentName()))){
        // to handle checkin document that with other file type
        extension=TextUtility.getExtension(dmsDocument.getVersionFileName());
      }else{
        extension=TextUtility.getExtension(dmsDocument.getDocumentName());
      }
      
      Class clsExtractor = (Class)hashExtractor.get(extension);

      //Special handling for scanning doc, since the file ext will be TIF/PDF, but the data is TEXT
      if (dmsDocument.getScanDocumentForFullText()){
        clsExtractor = StandardTextExtractor.class;
      }
      
      if(hashTmp.get("FULL_TEXT") != null){
        clsExtractor = StandardTextExtractor.class;
      }

      if (clsExtractor!=null) {
        try {
          String encoding = com.dcivision.framework.SystemParameterFactory.getSystemParameter(com.dcivision.framework.SystemParameterConstant.ENCODING);
          Constructor constructor = clsExtractor.getConstructor(new Class[]{java.io.InputStream.class, java.lang.String.class});
          extractor = (AbstractTextExtractor)constructor.newInstance(new Object[]{data, encoding});
          String text = extractor.getText();
          if (!Utility.isEmpty(text)) {
            doc.add(new Field("FULL_TEXT", text, Field.Store.YES, Field.Index.TOKENIZED));
          }
        } catch (java.lang.reflect.InvocationTargetException e) {
          log.error(e.getTargetException(), e.getTargetException());
        } catch (Exception e) {
          log.error(e, e);
        }
      }else{
        if((!Utility.isEmpty(dmsDocument.getVersionFileName())) && 
           (!dmsDocument.getVersionFileName().equals(dmsDocument.getDocumentName()))){
            // to handle checkin document that with other file type (graphical type)
            doc.add(new Field("FULL_TEXT", "", Field.Store.YES, Field.Index.TOKENIZED));
        }
      }
    }

    return(doc);
  }
  /**
   * Gets the correponding lucene document from dmsDocument object
   * @param dmsDocument the dmsDocument object
   * @param data        the InputStream of the document content
   * @return            Lucene document
   * @throws ApplicationException
   * @see #getIndexDocument(DmsDocument dmsDocument, InputStream data, String[] fields)
   */
  public static Document getIndexDocument(DmsDocument dmsDocument, InputStream data) throws ApplicationException {
    return(getIndexDocument(dmsDocument, data, null));
  }
}
